<?php
namespace app\manage\controller;
use app\manage\controller\Base;
use app\service\CateService;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\CateModel;

class CateController extends Base
{
    public function index(Request $request)
    {
		$key=$request->input('key','');
        View::assign('key',$key);
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$datasort=CateModel::where('catename','like','%'.$data['key'].'%')->order('id','ASC')->select();
            View::assign('datasort',$datasort);
    	}else{
    		$cate=new CateService();
			$datasort=$cate->tree();
            View::assign('datasort',$datasort);
    	}
		$count1=Db::name('cate')->count();
        View::assign('count1', $count1);
       	return view('cate/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->post();
		if($data['type']=='cate_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$cate=new CateModel();
			$info=$cate->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='cate_del'){
			if(Db::name('cate')->where('fid',$data['id'])->find()){
                return json(['code' => 2, 'message' => '拥有下一级']);
			}else{
				//删除图片(单页内容图片)
				if(CateModel::destroy($data['id'])){
                    return json(['code' => 1, 'message' => '操作成功']);
                } else {
                    return json(['code' => 0, 'message' => '操作失败']);
                }
			}
			
		}
		if($data['type']=='cate_start'){
			if(Db::name('cate')->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='cate_stop'){
			if(Db::name('cate')->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }
	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\CateValidate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/cate/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }
			$user = new CateModel($data);
			$res=$user->save();
			if($res){
				return view_s('添加成功',url('cate/index',['st'=>1]));
			}else{
				return view_e('栏目添加失败了');
			}
    	}
		$cate=new CateService();
		$datasort=$cate->tree();
        View::assign('datasort',$datasort);
		
		$dir=base_path().'/app/view/index';
		$wo=scandir($dir);
		$html=[];
		foreach($wo as $k=>$v){
			if(\is_file($dir.'/'.$v) and substr($v,0,2)!='m_'){
				$html[]=[
					'name'=>$v,
				];
			}
		}
        View::assign('html',array_reverse($html));
       return view('cate/add');
    }
	public function edit(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\CateValidate;
			if(!$validate->scene('edit')->check($data)){
				return view_e($validate->getError());
			}	
			if($data['id']==$data['fid']){
				return view_e('自己不能成为自己的下一级！');
			}
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/cate/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }
    		$user = new CateModel();
			$res=$user->update($data,['id' => $data['id']]);
			if($res){
				return view_s('修改成功',url('cate/index',['st'=>1]));
			}else{
				return view_e('栏目修改失败了');
			}
    	}
		
		$cid=$request->input('id',0);
		$data=Db::name('cate')->where('id',$cid)->find();
        View::assign('data',$data);
		$cate=new CateService();
		$datasort=$cate->tree();
        View::assign('datasort',$datasort);
		$dir=base_path().'/app/view/index';
		$wo=scandir($dir);
		$html=[];
		foreach($wo as $k=>$v){
			if(\is_file($dir.'/'.$v) and substr($v,0,2)!='m_'){
				$html[]=[
					'name'=>$v,
				];
			}
		}
        View::assign('html',array_reverse($html));
       return view('cate/edit');
    }
}
